package com.vote.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LotteryUtil {
	
	private static Random r = new Random();
	private static Log log = LogFactory.getLog(LotteryUtil.class);
	
	public static int getPrize(Map<String,Integer> map){		
		if (map == null){
			return 0;
		}		
		Integer level8 = map.get("8");
		Integer level7 = map.get("7");
		Integer level6 = map.get("6");
		Integer level5 = map.get("5");
		Integer level4 = map.get("4");
		Integer level3 = map.get("3");
		Integer level2 = map.get("2");
		Integer level1 = map.get("1");
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		if (level8!=null &&level8.intValue() != 0)
			list.add(level8);
		if (level7!=null &&level7.intValue() != 0)
			list.add(level7);
		if (level6!=null &&level6.intValue() != 0)
			list.add(level6);
		if (level5!=null &&level5.intValue() != 0)
			list.add(level5);
		if (level4!=null &&level4.intValue() != 0)
			list.add(level4);
		if (level3!=null &&level3.intValue() != 0)
			list.add(level3);
		if (level2!=null &&level2.intValue() != 0)
			list.add(level2);
		if (level1!=null &&level1.intValue() != 0)
			list.add(level1);
		
		
		int common = getN(list);
		//log.info("MIN:" + common);

		int first = 0;
		int second = 0;
		int third = 0;
		int four = 0;
		int fifth = 0;
		int sixth=0;
		int seventh=0;
		int eighth=0;
				
		if (level8!=null && level8.intValue() != 0) {
			first = common / level8.intValue();
		}
		
		if (level7!=null && level7.intValue() != 0) {
			second = first + (common / level7.intValue());
		} else
			second = first;
		
		if (level6!=null && level6.intValue() != 0) {
			third = second + (common / level6.intValue());
		} else
			third = second;
		
		if (level5!=null && level5.intValue() != 0) {
			four = third + (common / level5.intValue());
		} else
			four = third;
		
		if (level4!=null && level4.intValue() != 0) {
			fifth = four + (common / level4.intValue());
		} else
			fifth = four;
		
		if (level3!=null && level3.intValue() != 0) {
			sixth = fifth + (common / level3.intValue());
		} else
			sixth = fifth;
		
		if (level2!=null && level2.intValue() != 0) {
			seventh =  sixth+ (common / level2.intValue());
		} else
			seventh = sixth;
		
		if (level1!=null && level1.intValue() != 0) {
			eighth =  seventh+ (common / level1.intValue());
		} else
			eighth = sixth;	
		
		int ri = getRandom(common);
		
		if (ri >= 0 && ri < first && level8.intValue() != 0) {
			return 8;
		}else if (ri >= first && ri < second && level7.intValue() != 0) {
			return 7;
		}  else if (ri >= second && ri < third && level6.intValue() != 0) {
			return 6;
		} else if (ri >= third && ri < four && level5.intValue() != 0) {
			return 5;
		} else if (ri >= four && ri < fifth && level4.intValue() != 0) {
			return 4;
		}else if (ri >= fifth && ri < sixth && level3.intValue() != 0) {
			return 3;
		}else if (ri >= sixth && ri < seventh && level2.intValue() != 0) {
			return 2;
		}else if (ri >= seventh && ri < eighth && level1.intValue() != 0) {
			return 1;
		}else {
			return 0;
		}
	}
	
	public static int gcd(int z, int y) {
		while (true) {
			if ((z = z % y) == 0)
				return y;
			if ((y = y % z) == 0)
				return z;
		}
	}

	public static int gys(int z, int y) {
		int t = 0;
		int c = 0;
		c = gcd(z, y);
		t = z * y / c;

		return t;
	}

	public static int getN(ArrayList list) {
		int t = 1;
		for (int i = 0; i < list.size(); i++) {
			Integer temp = (Integer) list.get(i);
			t = gys(t, temp.intValue());
		}
		return t;
	}

	public static int getRandom(int y) {
		int result = r.nextInt(y);

		return result;
	}
	
	public static void main(String[] args) {
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("1", 1);
		map.put("2", 200);
		map.put("3", 400);
		map.put("4", 800);
		map.put("5", 1200);
		map.put("6", 3000);
		map.put("7", 8000);
		map.put("8", 10000);	
		
		int cnt=0;
		for(int i=0;i<100000;i++){
			int s=LotteryUtil.getPrize(map);
			if(s==0){
				log.info("i:"+ i +" s:"+s);
				cnt++;				
			}
		}
		System.out.println("cnt:"+cnt);
	}

}